# -*- coding: utf-8 -*-
# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python
"""
test resnet50 model
"""

import os
import sys
import logging
import tarfile
import six
import cv2
import wget
import pytest
import numpy as np

from img_preprocess import preprocess

# pylint: disable=wrong-import-position
sys.path.append("..")
from test_case import InferenceTest

# pylint: enable=wrong-import-position


def check_model_exist():
    """
    check model exist
    """
    resnet50_url = "https://bj.bcebos.com/paddlehub/fastdeploy/DenseNet121_infer.tgz"
    if not os.path.exists("./DenseNet121_infer/inference.pdiparams"):
        wget.download(resnet50_url, out="./")
        tar = tarfile.open("DenseNet121_infer.tgz")
        tar.extractall()
        tar.close()


@pytest.mark.p0
@pytest.mark.config_init_combined_model
def test_config():
    """
    test combined model config
    """
    check_model_exist()
    test_suite = InferenceTest()
    test_suite.load_config(model_file="./DenseNet121_infer/inference.pdmodel", params_file="./DenseNet121_infer/inference.pdiparams")
    test_suite.config_test()


@pytest.mark.p0
@pytest.mark.config_disablegpu_memory
def test_disable_gpu():
    """
    test no gpu resources occupied after disable gpu
    """
    check_model_exist()
    test_suite = InferenceTest()
    test_suite.load_config(model_file="./DenseNet121_infer/inference.pdmodel", params_file="./DenseNet121_infer/inference.pdiparams")
    batch_size = 1
    img = cv2.imread("ILSVRC2012_val_00000010.jpeg")
    origin_shape = img.shape
    print(origin_shape)
    input_shape = [[1, 3, 224, 224], [1, 2]]
    target_size = input_shape[0][2:4]
    img = preprocess(img, target_size=target_size)
    print(img.shape)
    scale_factor = np.array([[origin_shape[0] / target_size[0], origin_shape[1] / target_size[1]]]).astype(
        'float32')
    input_data_dict = {
        "inputs": img,
        "scale_factor": scale_factor,
        "im_shape": np.array(origin_shape[:2]).astype("float32"),
    }
    test_suite.disable_gpu_test(input_data_dict)


@pytest.mark.p1
@pytest.mark.gpu_bz1_precision
def test_gpu_bz1():
    """
    compared trt gpu batch_size=1-10 resnet50 outputs with true val
    """
    check_model_exist()

    model_file = "./DenseNet121_infer/inference.pdmodel"
    params_file = "./DenseNet121_infer/inference.pdiparams"

    test_suite = InferenceTest()
    test_suite.load_config(model_file=model_file, params_file=params_file)

    img = cv2.imread("ILSVRC2012_val_00000010.jpeg")
    origin_shape = img.shape
    print(origin_shape)
    input_shape = [[1, 3, 224, 224], [1, 2]]
    target_size = input_shape[0][2:4]
    img = preprocess(img, target_size=target_size)
    print(img.shape)
    scale_factor = np.array([[origin_shape[0] / target_size[0], origin_shape[1] / target_size[1]]]).astype(
        'float32')
    input_data_dict = {
        "inputs": img,
        "scale_factor": scale_factor,
        "im_shape": np.array(origin_shape[:2]).astype("float32"),
    }
    output_data_dict = test_suite.get_truth_val(input_data_dict, device="gpu")

    del test_suite  # destroy class to save memory

    test_suite2 = InferenceTest()
    test_suite2.load_config(model_file=model_file, params_file=params_file)
    test_suite2.gpu_more_bz_test(input_data_dict, output_data_dict, delta=1e-5)

    del test_suite2  # destroy class to save memory


